package com.yonyou.pmclouds.basecom.util;

import com.yonyou.pmclouds.basecom.ref.itf.TreeVODependencyitf;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author chulf
 * @date 2020/3/12
 * @Des  多向链表构建器
 * @Des  如果出现链表循环则有问题  暂时先不用
 */

public class MultilinkedListResloverUtil {

    public static Node getNodeResult(TreeVODependencyitf treeVO, Map<String,TreeVODependencyitf> allDatas) {

        TreeVODependencyitf[] treeVOS = treeVO.findChildren();

        ArrayList<Node> nodes = new ArrayList<>();

        String[] otheDependency = treeVO.findOtheDependency();

        if (!ArrayUtils.isEmpty(treeVOS)) {
            for (TreeVODependencyitf tempTreeVO : treeVOS) {
                Node nodeResult = getNodeResult(tempTreeVO,allDatas);
                nodes.add(nodeResult);
            }
        } else if (!ArrayUtils.isEmpty(otheDependency)) {
            for (String dependencyPk : otheDependency) {
                TreeVODependencyitf treeVODependencyitf = allDatas.get(dependencyPk);
                if(treeVODependencyitf != null){
                    Node nodeResult = getNodeResult(treeVODependencyitf,allDatas);
                    nodes.add(nodeResult);
                }
            }
        } else {
            return new Node(treeVO.findPk(), null);
        }

        return new Node(treeVO.findPk(),ArrayUtils.toArray(new Node[0]));
    }
}

@Data
class Node{
    String nodePk;
    Node[] Nodes;
    public Node(String pk,Node[] nodes){
        this.nodePk = pk;
        this.Nodes = nodes;
    }
}